﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using TickSharp.Core.Interfaces;
using TickSharp.Core.Model;

namespace TickSharp.Core.Infrastructure
{
    public class ProjectRepository : RepositoryBase
    {
        public ProjectRepository(IApiConnectionInfo apiConnectionInfo) : base(apiConnectionInfo)
        {
        }

        /// <summary>
        /// Optional parameters:
        /// project_id
        /// open (true|false)
        /// project_billable (true|false)
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Project> List()
        {
            string uri = BuildUrl("projects", null);

            XElement elements = GetResponse(uri);

            var projects = from e in elements.Elements()
                           where e.Name.LocalName == "project"
                               select new Project
                               {
                                   Id = int.Parse(e.Element("id").Value),
                                   Name = e.Element("name").Value,
                                   ClientName = e.Element("client_name").Value
                               };
            return projects;
        }
    }
}
